package pl.edu.mimuw.irs.core.model.design.net;

import java.util.LinkedHashMap;
import java.util.Map;

import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException;
import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException.DesignCause;

public class IRSTransitionCopier extends AbstractIRSNodeCopier {

	// *** Attributes

	protected IRSTransition transition;
	protected double chance;
	public IRSTransition getTransition() {
		return transition;
	}
	public double getChance() {
		return chance;
	}
	
	// *** Constructors
	
	public IRSTransitionCopier(IRSTransition transition) {
		super(transition);
		this.transition = transition; 
		this.chance = transition.getChance();
	}
	
	// *** Methods

	protected IRSNode createPlainCopy(Map<IRSNode, IRSNode> clonesMap) {
		IRSTransition clone = new IRSTransition(getIdManager(), 
				getIdManager().getPrefixedNewId(getId()), getNodeType(), getPosition().clone());
		clone.setName(getName());
		return clone;
	}
	
	@Override
	public void copyAttributesTo(IRSNode target) {
		if (!(target instanceof IRSTransition)) 
			throw new InternalDesignException(DesignCause.INCOMPATIBLE_COPY_NODES, getNode(), target);
		IRSTransition targetTransition = (IRSTransition) target;
		targetTransition.setChance(getChance());
	}

	public IRSActivity copyAsActivity() {
		return copyAsActivity(CLONE_INCOMING_ARCS | CLONE_OUTGOING_ARCS);
	}
	
	public IRSActivity copyAsActivity(int options) {
		Map<IRSNode, IRSNode> clonedMap = new LinkedHashMap<IRSNode, IRSNode>();
		IRSActivity clone = copyAsActivity(clonedMap, (SHALLOW & options) == 0);
		if ((CLONE_INCOMING_ARCS & options) != 0) cloneIncomingArcs(getNode(), clone, clonedMap);
		if ((CLONE_OUTGOING_ARCS & options) != 0) cloneOutgoingArcs(getNode(), clone, clonedMap);
		if ((OMIT_NET_ASSIGNMENT & options) == 0) assignNet(clone);
		return clone;
	}
	
	protected IRSActivity copyAsActivity(Map<IRSNode, IRSNode> clonesMap, boolean deep) {
		IRSActivity clone = new IRSActivity(getIdManager(),
				getIdManager().getPrefixedNewId(getId()), getNodeType(), getPosition().clone());
		clone.setName(getName());
		if (deep) copyContents(getNode(), clone, clonesMap);
		//if (getNet() != null) getNet().addTransition(clone);
		return clone;
	}

	public IRSTransition copyAsTransition() {
		return copyAsTransition(CLONE_INCOMING_ARCS | CLONE_OUTGOING_ARCS);
	}
	
	public IRSTransition copyAsTransition(int options) {
		Map<IRSNode, IRSNode> clonedMap = new LinkedHashMap<IRSNode, IRSNode>();
		IRSTransition clone = copyAsTransition(clonedMap, (SHALLOW & options) == 0);
		if ((CLONE_INCOMING_ARCS & options) != 0) cloneIncomingArcs(getNode(), clone, clonedMap);
		if ((CLONE_OUTGOING_ARCS & options) != 0) cloneOutgoingArcs(getNode(), clone, clonedMap);
		if ((OMIT_NET_ASSIGNMENT & options) == 0) assignNet(clone);
		return clone;
	}
	
	protected IRSTransition copyAsTransition(Map<IRSNode, IRSNode> clonesMap, boolean deep) {
		IRSTransition clone = new IRSTransition(getIdManager(),
				getIdManager().getPrefixedNewId(getId()), getNodeType(), getPosition().clone());
		clone.setName(getName());
		if (deep) copyContents(getNode(), clone, clonesMap);
		//if (getNet() != null) getNet().addTransition(clone);
		return clone;
	}
}
